<?php
/*
 * 后台首页设置控制器
 * @author     team
 * @Created  2016/6/17
 */
namespace Admin\Controller;
// use Think\Controller;
class SettingController extends CommonController {


   //加载编辑页面

	public function edit() {
		$mod = M("Setting");
		//获取对应模块设置
		$list = $mod->where("item=0")->select();
		//遍历获取的信息
		foreach ($list as $k => $v) {
			//将表中的键值对封装为数组
			$data[$v["itemkey"]] = $v["itemvalue"];
		}
		$this->assign("data", $data);
		$this->display();
	}

	//执行修改

	public function update() {
		$mod = M("Setting");
		$mod->where('item=0')->delete();
		//将获取的信息遍历
		foreach (I('post.') as $k => $v) {
			//将获取的信息中的键封装进itemkey字段
			$data['itemkey']   = $k;
			//将获取的信息中的值封装进itemvalue字段
			$data['itemvalue'] = $v;
			$mod->add($data);
		}
		$this->redirect("setting/edit");
	}


	//修改个人密码

	public function inbox() {

		$this->display();
	}

	public function updatepw() {
		$mod  = M("Admin");
		//根据用户名找到对应的用户信息

		$data = $mod->where([ 'username'=>$_COOKIE['username'] ])->find();

		//var_dump($data);//"admin" "07b19e2d0b2291ef6b6bebfc7dde7ddd" "hdkdhkdk"
		//var_dump(I("password"));//123  123456
		$repassword = I('repassword');
		$oldpassword = I('oldpassword');

        if( empty( I(password) ) ){
						$this->error('密码不能为空','',2); exit;
		}
        //判断oldpassword 和 数据库中password 是否相同
        if ( md5($oldpassword.$data['salt']) !== $data['password']) {
        	$this->error("原密码错误", U("Admin/Setting/inbox"));exit;
        }
        //判断oldpassword 和 输入的 password 是否不相同
		if ( $oldpassword == I('password')  ) {
			$this->error("新密码未重置", U("Admin/Setting/inbox"));exit;
		}
		//判断 repassword 和输入的 password 是否相同
		if ($repassword == I('password')) {
			$mod->where([ 'username'=>$_COOKIE['username'] ])->setField("password", md5(I('password').$data['salt']));
			$this->success("密码修改成功,请妥善保管您的新密码！", U("Admin/Setting/inbox"));
		}else{
			$this->error("重复新密码错误", U("Admin/Setting/inbox"));exit;
		}


}

	// 初始化


	//删除缓存Runtime文件夹

	public function  clearCache() {
		if (file_exists(RUNTIME_PATH)) {
			$dir = new \Common\Util\File(RUNTIME_PATH);
			if ($dir->rmdirs()) {
				$this->success('缓存清除成功！');
			};
		}
	}
}
